package com.action;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.beans.StudentInfo;
import com.service.StudentService;



@Controller
public class StudentAction {

	// 用于国际化
	@Resource
	private MessageSource messageSource;
	//学生
	@Resource
	private StudentService studentService;

	// 注意,这里配置了访问 login.html 在这处理
	@GetMapping(value = { "/login" })
	public String gotoLogin() {
		return "login";
	}

	// 登录跳转
	@PostMapping(value = { "/login" })
	public String login(String stuName, String password, ModelMap m, HttpServletRequest request) {
		StudentInfo stu = studentService.getUserByName(stuName);
		if (stu != null) {
			if (password.equals(stu.getPassword())) {
				request.getSession().setAttribute("session_stu", stu);
				return "main";
			}else {
				request.setAttribute("msg", "密码错误");
				return "login";}
		}else {
			Locale locale = LocaleContextHolder.getLocale();
			String msg = messageSource.getMessage("loginerror", null, locale);
			m.put("msg", msg);
			return "login";
		}
	}

	// 查询所有用户列表
	@GetMapping("/users")
	public String findAll(ModelMap map) {
		HashMap<String, Object> paramMap = null;
		map.put("usersList", studentService.getStuList(paramMap));
		return "user-pages/user-manager";
	}

	// 转到用户添加界面
	@GetMapping("/user")
	public String toAddPage() {
		return "user-pages/user-add";
	}

	// 真正的进行用户添加
	@PostMapping("/add")
	public String addUser(ModelMap m, @ModelAttribute ("user")  @Validated StudentInfo user, BindingResult result) {
		if(result.hasErrors()) {
			m.put("msg", "校验失败");
			List<FieldError> errorList=result.getFieldErrors();
			errorList.forEach(e->System.out.println(e.getField()+":"+e.getDefaultMessage()));	
			return "user-pages/user-add";
		}else {
			user.setStuCreateTime(LocalDateTime.now());
			studentService.addStu(user);
			return "redirect:/users";
		}
		
	}

	// 来到修改页面，查出当前用户，在页面回显
	@GetMapping("/user/{id}")
	public String toEditPage(@PathVariable("id") Integer id, Model model) {
		StudentInfo user = studentService.getStuById(id);
		model.addAttribute("user", user);
		return "user-pages/user-edit";
	}

	/*
	 * 删除
	 */
	@GetMapping("/delete/{id}")
	public String deleteUser(@PathVariable("id") Integer id) {
		studentService.deleteStuById(id);
		return "redirect:/users";
	}

	/*
	 * 下两面两个方式, 要配置: spring.mvc.hiddenmethod.filter.enabled=true //用户修改；需要提交员工id
	 */
	@PutMapping("/edit")
	public String updateUser( StudentInfo stu) {
		stu.setStuUpdateTime(LocalDateTime.now());
		studentService.saveStu(stu);
		return "redirect:/users";
	}

}
